import {messages, deepFullCopy} from "share/common";
/**
 * Created by tanbingqin on 2018/6/7.
 */
import React from 'react'
import {connect} from 'react-redux'
import { Button, Input, Radio, Row, Col, message, Icon, Checkbox } from 'antd'
import formService from 'containers/setting/form/form.service'
import baseService from 'share/base.service'
import menuRoute from 'routes/menuRoute'
import Chooser from 'components/chooser'
import config from 'config'
import chooserData from 'share/chooserData'
import PermissionsAllocation from 'components/template/permissions-allocation'

const RadioGroup = Radio.Group;

class FormPermission extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading1: false,
      isEdit1: false,
      loading2: false,
      isEdit2: false,
      loading3: false,
      isEdit3: false,
      loading4: false,
      isEdit4: false,
      userValue: {
        type: 'all',
        values: []
      },
      canEditExpense: true, //是否可更改分配人员 这个为false说明当前的报销单是由申请单创建的时候关联创建的，分配费用依据此字段多展示个：同关联申请单费用
      expenseTypeValue: {
        type: 1001
      },
      fatherExpenseType: [], //所属父类型，用于分配费用中下拉框的数据，不能直接在弹框里渲染，因为chrome和opera会有Background Tabs Throttling
      expenseTypeCategory: [], //费用大类，不直接在弹框中渲染，理由同上
      visibleCompanyScope: 1, //1全部公司 2部分公司
      companySelectedList: [], //分配公司，如果是部分公司，则选中的公司对象
      visibleLoanScope: 1003, //1001多行借款单全部类型 1002部分类型／自定义 1003单行借款单默认类型
      loanSelectedList: [], //自定义费用，如果是部分费用，则选中的费用对象
      parentEnabled: false, //是否开启父子类型模式
      visibleExpenseScope: 1001, //1001全部费用 1002部分费用
      expenseSelectedList: [] //自定义费用，如果是部分费用，则选中的费用对象
    }
  }

  componentWillMount() {
    this.initCompanyData();
    this.initUserData();
    this.initEditExpense();
    this.initExpenseData();
    this.initFatherExpenseType();
    this.initExpenseTypeCategory();
    this.initLoanTypeData();
  }

  componentDidMount() {

  }

  //获取所属父类型
  initFatherExpenseType = () => {
    let setOfBooksId = this.props.tenantMode ? this.context.booksID : this.props.company.setOfBooksId;
    let fatherExpenseType = [];
    formService.getFatherExpenseType(setOfBooksId).then(res => {
      if (res.data && res.data.length) {
        res.data.map(expense => {
          fatherExpenseType.push({label: expense.name, value: expense.id});
        });
        this.setState({fatherExpenseType});
      }
    }).catch(error => {
      console.log(error);
    });
  };

  //获取所有费用大类
  initExpenseTypeCategory = () => {
    let setOfBooksId = this.props.tenantMode ? this.context.booksID : this.props.company.setOfBooksId;
    let expenseTypeCategory = [];
    baseService.getExpenseTypeCategory(setOfBooksId).then(res => {
      if (res.data && res.data.length) {
        res.data.map(category => {
          expenseTypeCategory.push({label: category.name, value: category.id});
        });
        this.setState({expenseTypeCategory});
      }
    }).catch(error => {
      console.log(error);
    });
  };

  //初始化分配公司数据，点击取消还原数据的时候也使用此初始化方法
  initCompanyData = () => {
    const {form} = this.context;
    let visibleCompanyScope = 1;
    if (form.visibleCompanyScope === 2) {
      visibleCompanyScope = 2;
    }
    let companySelectedList = [];
    if (form.companyList && form.companyList.length) {
      companySelectedList = form.companyList;
    }
    this.setState({visibleCompanyScope, companySelectedList});
  };

  //初始化分配人员数据，点击取消还原数据的时候也使用此初始化方法
  initUserData = () => {
    const {userScope} = this.context;
    //分配人员数据初始化
    let permissionValueType;
    switch (userScope.visibleScope) {
      case 1001:
        permissionValueType = 'all';
        break;
      case 1002:
        permissionValueType = 'group';
        break;
      case 1003:
        permissionValueType = 'department';
        break;
    }
    let userSelectedList = [];
    let userValue = {
      type: permissionValueType,
      values: userSelectedList
    };
    if (userScope.departments) {
      userScope.departments.map(department => {
        userSelectedList.push({
          label: department.path,
          key: department.departmentOID,
          value: department.departmentOID
        })
      });
      userValue.values = userSelectedList;
    }
    if (userScope.userGroups) {
      userScope.userGroups.map(userGroup => {
        userSelectedList.push({
          label: userGroup.name,
          key: userGroup.userGroupOID,
          value: userGroup.userGroupOID
        })
      });
      userValue.values = userSelectedList;
    }
    this.setState({userValue});
  };

  //初始化是否可分配费用
  initEditExpense = () => {
    const {form} = this.context;
    if ((form.formType === 3001 || form.formType === 3002 || form.formType === 3003) && form.referenceOID) {
      this.setState({
        canEditExpense: false
      });
    } else {
      this.setState({
        canEditExpense: true
      });
    }
  };

  //初始化分配费用数据，点击取消还原数据的时候也使用此初始化方法
  initExpenseData = () => {
    const {form, userScope, expenseTypeScope} = this.context;
    let visibleExpenseScope = expenseTypeScope.visibleScope;
    let parentEnabled = !!expenseTypeScope.parentEnabled;
    let expenseSelectedList = [];
    if (expenseTypeScope.expenseTypes && expenseTypeScope.expenseTypes.length) {
      expenseTypeScope.expenseTypes.map((item) => {
        expenseSelectedList.push(item);
      });
    }
    this.setState({parentEnabled, visibleExpenseScope, expenseSelectedList});
  };

  //初始化分配借款类型数据，点击取消还原数据的时候也使用此初始化方法
  initLoanTypeData = () => {
    const {formType, loanTypeScope} = this.context;
    let visibleLoanScope = loanTypeScope.visibleScope;
    let loanSelectedList = [];
    if (loanTypeScope.loanTypes && loanTypeScope.loanTypes.length) {
      loanTypeScope.loanTypes.map((item) => {
        loanSelectedList.push(item);
      });
    }
    this.setState({visibleLoanScope, loanSelectedList});
  };

  handleChangeCompany = (e) => {
    this.setState({
      visibleCompanyScope: e.target.value
    }, () => {
      this.setState({companySelectedList: []});
    });
  };

  handleSelectCompany = (value) => {
    this.setState({
      companySelectedList: value
    });
  };

  handleChangeParentEnabled = (value) => {
    this.setState({
      parentEnabled: value
    });
  };

  handleChangeExpense = (e) => {
    this.setState({
      visibleExpenseScope: e.target.value
    }, () => {
      this.setState({expenseSelectedList: []});
    });
  };

  handleChangeLoan = (e) => {
    this.setState({
      visibleLoanScope: e.target.value
    }, () => {
      this.setState({loanSelectedList: []});
    });
  };

  handleSelectExpense = (value) => {
    this.setState({
      expenseSelectedList: value
    });
  };

  handleSelectLoan = (value) => {
    this.setState({
      loanSelectedList: value
    });
  };

  handleChangePermissions = (values) => {
    this.setState({userValue: values});
  };

  //type company公司 user人员 expense费用 loan借款类型
  //是否是点击 取消，bool，如果为true就要还原数据
  handleChangeStatus = (type, isCancel) => {
    if (type === 'company') {
      this.setState({
        isEdit1: !this.state.isEdit1
      });
      if (isCancel) {
        this.initCompanyData();
      }
    }
    if (type === 'user') {
      this.setState({
        isEdit2: !this.state.isEdit2
      });
      if (isCancel) {
        this.initUserData();
      }
    }
    if (type === 'expense') {
      this.setState({
        isEdit3: !this.state.isEdit3
      });
      if (isCancel) {
        this.initExpenseData();
      }
    }
    if (type === 'loan') {
      this.setState({
        isEdit4: !this.state.isEdit4
      });
      if (isCancel) {
        this.initLoanTypeData();
      }
    }
  };

  //type company公司 user人员 expense费用
  handleSave = (type) => {
    const { visibleCompanyScope, companySelectedList, userValue, visibleExpenseScope, expenseSelectedList, parentEnabled,
      visibleLoanScope, loanSelectedList } = this.state;
    const { form } = this.context;
    if (type === 'company') {
      //校验分配公司的选择
      if (visibleCompanyScope === 2 && !companySelectedList.length) {
        message.error(messages('setting.key1547')/*请选择要分配的公司*/);//请选择要分配的公司
        return
      }
      let permissionData = {};
      permissionData.customFormOID = form.formOID;
      permissionData.visibleCompanyScope = visibleCompanyScope;
      permissionData.companyOIDList = [];
      if (permissionData.visibleCompanyScope === 2) {
        companySelectedList.map((item) => {
          permissionData.companyOIDList.push(item.companyOID);
        });
      }
      this.setState({loading1: true});
      formService.setCompanyPermission(permissionData).then(res => {
        message.success(messages("common.save.success", {name: ''}));
        this.setState({loading1: false});
        this.handleChangeStatus('company');
        this.props.refreshData('company');
      }).catch(error => {
        this.setState({loading1: false});
        let errorMessage = messages('common.error');
        if (error.response && error.response.data && error.response.data.message) {
          errorMessage = error.response.data.message;
        }
        message.error(errorMessage);
      });
    }
    if (type === 'user') {
      //校验分配人员的选择
      if (userValue.type !== 'all' && !userValue.values.length) {
        message.error(messages('setting.key1548')/*请选择要分配的人员*/);//请选择要分配的人员
        return
      }
      let permissionData = {};
      permissionData.formOID = form.formOID;
      switch (userValue.type) {
        case 'all':
          permissionData.visibleScope = 1001;
          break;
        case 'group':
          permissionData.visibleScope = 1002;
          permissionData.userGroupOIDs = [];
          permissionData.userGroups = [];
          userValue.values.map(group => {
            permissionData.userGroupOIDs.push(group.key);
            permissionData.userGroups.push({
              userGroupOID: group.key,
              name: typeof (group.label) == 'string' ? group.label : group.label.props.children[0]
            })
          });
          break;
        case 'department':
          permissionData.visibleScope = 1003;
          permissionData.departmentOIDs = [];
          permissionData.departments = [];
          userValue.values.map(department => {
            permissionData.departmentOIDs.push(department.value);
            permissionData.departments.push({
              departmentOID: department.key,
              path: department.label
            })
          });
          break;
      }
      this.setState({loading2: true});
      formService.updateUserScope(permissionData).then(res => {
        message.success(messages("common.save.success", {name: ''}));
        this.setState({loading2: false});
        this.handleChangeStatus('user');
        this.props.refreshData('user');
      }).catch(error => {
        this.setState({loading2: false});
        let errorMessage = messages('common.error');
        if (error.response && error.response.data && error.response.data.message) {
          errorMessage = error.response.data.message;
        }
        message.error(errorMessage);
      });
    }
    if (type === 'expense') {
      //校验分配费用的选择
      if (visibleExpenseScope === 1002 && !expenseSelectedList.length) {
        message.error(messages('setting.key1549')/*请选择要分配的费用*/);//请选择要分配的费用
        return
      }
      this.setState({loading3: true});
      formService.updateExpenseTypeScope(form.formOID, expenseSelectedList, visibleExpenseScope, false).then(res => {
        message.success(messages("common.save.success", {name: ''}));
        this.setState({loading3: false});
        this.handleChangeStatus('expense');
        this.props.refreshData('expense');
      }).catch(error => {
        this.setState({loading3: false});
        let errorMessage = messages('common.error');
        if (error.response && error.response.data && error.response.data.message) {
          errorMessage = error.response.data.message;
        }
        message.error(errorMessage);
      });
    }
    if (type === 'loan') {
      //校验分配借款类型的选择
      if (visibleLoanScope === 1002 && !loanSelectedList.length) {
        message.error(messages('setting.key1550')/*请选择要分配的借款类型*/);
        return
      }
      this.setState({loading4: true});
      formService.updateLoanTypeScope(form.formOID, loanSelectedList, visibleLoanScope).then(res => {
        message.success(messages("common.save.success", {name: ''}));
        this.setState({loading4: false});
        this.handleChangeStatus('loan');
        this.props.refreshData('loan');
      }).catch(error => {
        this.setState({loading4: false});
        let errorMessage = messages('common.error');
        if (error.response && error.response.data && error.response.data.message) {
          errorMessage = error.response.data.message;
        }
        message.error(errorMessage);
      });
    }
  };

  renderSobUser=()=>{
    return (
      <Row>
        <Col span={16} offset={3}>
          <div className="user-list-wrap-title">
            <Icon type="info-circle"/>
            <span>{messages('setting.key626'/*如需编辑适用人员，请前往相关联的申请单设置*/)}</span>
          </div>
          <div className="user-list-wrap">
            {this.renderUser(this.context.userScope)}
          </div>
        </Col>
      </Row>
    )
  }
  renderTenantUser = () => {
    const { loading1, isEdit1, loading2, isEdit2, loading3, isEdit3, companySelectedList,
      visibleCompanyScope, expenseSelectedList, visibleExpenseScope, userValue } = this.state;
    const { form } = this.context;
    return (
      <div>
        <Row>
          <Col span={8} offset={3}>
            <PermissionsAllocation onChange={this.handleChangePermissions}
                                   disabled={!isEdit2}
                                   mode="oid"
                                   needEntity
                                   hiddenComponents={this.props.tenantMode ? ["department"] : []}
                                   value={userValue}/>
          </Col>
        </Row>
        {form.visibleLoanTypeScope !== 1004 && (
          <Row style={{marginTop: 12, marginBottom: 20}}>
            <Col span={8} offset={3}>
              {!isEdit2 && (
                <Button type="primary" onClick={() => this.handleChangeStatus('user')}>{messages('common.edit')/*编辑*/}</Button>
              )}
              {isEdit2 && (
                <div>
                  <Button style={{marginRight: 20}} type="primary" loading={loading2} onClick={() => this.handleSave('user')}>{messages('common.save')/*保存*/}</Button>
                  <Button onClick={() => this.handleChangeStatus('user', true)}>{messages('common.cancel')/*取消*/}</Button>
                </div>
              )}
            </Col>
          </Row>
        )}
      </div>
    )
  }
  renderSobExpenseTypeScope=()=>{
    return (
      <Row>
        <Col span={16} offset={3}>
          <div className="expense-type-list-wrap-title">
            <Icon type="info-circle"/>
            <span>{messages('setting.key627'/*如需编辑可见费用类型，请前往相关联的申请单设置*/)}</span>
          </div>
          <div className="expense-type-list-wrap">
            {this.renderExpenseTypeScope(this.context.expenseTypeScope)}
          </div>
        </Col>
      </Row>
    )
  }
  renderTenantExpenseTypeScope=()=>{
    const { loading1, isEdit1, loading2, isEdit2, loading3, isEdit3, companySelectedList, visibleCompanyScope,
      expenseSelectedList, visibleExpenseScope, userValue, fatherExpenseType, expenseTypeCategory, parentEnabled, canEditExpense } = this.state;
    const { form } = this.context;
    let chooserItem = deepFullCopy(chooserData['available_expense']);
    let setOfBooksId = this.props.tenantMode ? this.context.booksID : this.props.company.setOfBooksId;
    let selectItem = {
      type: 'select',
      id: 'parentId',
      label: messages('setting.key529')/*所属父类型*/,
      options: fatherExpenseType
    };
    let selectItem2 = {
      type: 'select',
      id: 'expenseTypeCategoryId',
      label: messages('setting.key1767')/*费用大类*/,
      options: expenseTypeCategory
    };
    chooserItem.searchForm.push(selectItem);
    chooserItem.searchForm.push(selectItem2);
      return (
        <div>
          <Row>
            <Col span={8} offset={3}>
              <RadioGroup onChange={this.handleChangeExpense} value={visibleExpenseScope}>
                {!canEditExpense && (
                  <Radio disabled={!isEdit3} value={1004}>{messages('setting.key2184')/*同关联申请单费用*/}</Radio>
                )}
                <Radio disabled={!isEdit3} value={1001}>{messages('setting.key628'/*全部费用*/)}</Radio>
                <Radio disabled={!isEdit3} value={1002}>{messages('setting.key629'/*部分费用*/)}</Radio>
              </RadioGroup>
              {visibleExpenseScope === 1002 &&
              <Chooser placeholder={messages('common.please.select')}
                       selectorItem={chooserItem}
                       labelKey='name'
                       valueKey='expenseTypeOID'
                       listExtraParams={{
                         setOfBooksId: this.props.tenantMode ? this.context.booksID : this.props.company.setOfBooksId,
                         enabled: true
                       }}
                       disabled={!isEdit3}
                       onChange={this.handleSelectExpense}
                       value={expenseSelectedList}
                       showNumber={true}
                       single={false}/>
              }
            </Col>
          </Row>
          {/*开启父子类型不上，差补费用的父子类型属性在设计上没考虑清楚，待设计*/}
          {false && form.referenceOID && (
            <Row style={{marginTop: 8}}>
              <Col span={21} offset={3}>
                <Checkbox
                  disabled={!isEdit3}
                  checked={parentEnabled}
                  onChange={e => this.handleChangeParentEnabled(e.target.checked)}
                >
                  {messages('setting.key2110')/*开启父子类型模式*/}
                </Checkbox>
                <div style={{color: '#989898', marginTop: 10}}>
                  {messages('setting.key2111')/*开启后，申请单只能填写可见费用类型中的父类型，对应的报销单只能填写可见费用类型中的子类型。*/}
                </div>
              </Col>
            </Row>
          )}
          <Row style={{marginTop: 12, marginBottom: 20}}>
            <Col span={8} offset={3}>
              {!isEdit3 && (
                <Button type="primary" onClick={() => this.handleChangeStatus('expense')}>{messages('common.edit')/*编辑*/}</Button>
              )}
              {isEdit3 && (
                <div>
                  <Button style={{marginRight: 20}} type="primary" loading={loading3} onClick={() => this.handleSave('expense')}>{messages('common.save')/*保存*/}</Button>
                  <Button onClick={() => this.handleChangeStatus('expense', true)}>{messages('common.cancel')/*取消*/}</Button>
                </div>
              )}
            </Col>
          </Row>
        </div>
      )
  }

  renderLoanTypeScope = (loanType) => {
    const {formType} = this.context;
    if(loanType.visibleScope === 1001 && formType === 2005){
      return (
        <div>{messages('setting.key1551')/*系统默认值*/}</div>
      )
    }
    if(loanType.visibleScope === 1001 && formType === 2006){
      return (
        <div>{messages('common.all.type')/*全部类型*/}</div>
      )
    }
    if(loanType.visibleScope === 1002){
      let len = this.state.loanSelectedList.length;
      return this.state.loanSelectedList.map((item,index)=>{
        if(len-1===index){
          return (<div className="f-left">{item.description}</div>);
        }else {
          return (<div className="f-left">{item.description}，</div>);
        }
      })
    }
  };

  renderExpenseTypeScope = (expenseType) =>{
    if(expenseType.visibleScope === 1001){
      return (
        <div>{messages('setting.key628'/*全部费用*/)}</div>
      )
    }
    if(expenseType.visibleScope === 1002){
      let len = this.state.expenseSelectedList.length;
      return this.state.expenseSelectedList.map((item,index)=>{
        if(len-1===index){
          return (<div className="f-left">{item.name}</div>);
        }else {
          return (<div className="f-left">{item.name}，</div>);
        }
      })
    }
  }

  //集团模式下的表单或者公司模式下公司自己的表单
  renderTenantLoanTypeScope = () => {
    const { loading4, isEdit4, visibleLoanScope, loanSelectedList } = this.state;
    const { formType, form } = this.context;
    let chooserItem = deepFullCopy(chooserData['available_loan_type']);
    let setOfBookId = this.props.tenantMode ? this.context.booksID : this.props.company.setOfBooksId;
    chooserItem.url += `/${setOfBookId}`;
    return (
      <div>
        <Row>
          <Col span={8} offset={3}>
            {formType === 2005 && (
              <RadioGroup onChange={this.handleChangeLoan} value={visibleLoanScope}>
                <Radio disabled={!isEdit4} value={1003}>{messages('setting.key1551')/*系统默认值*/}</Radio>
                <Radio disabled={!isEdit4} value={1002}>{messages('setting.key1415')/*自定义*/}</Radio>
              </RadioGroup>
            )}
            {formType === 2006 && (
              <RadioGroup onChange={this.handleChangeLoan} value={visibleLoanScope}>
                <Radio disabled={!isEdit4} value={1001}>{messages('common.all.type')/*全部类型*/}</Radio>
                <Radio disabled={!isEdit4} value={1002}>{messages('setting.key1552')/*部分类型*/}</Radio>
              </RadioGroup>
            )}
            {visibleLoanScope === 1002 &&
            <Chooser placeholder={messages('common.please.select')}
                     selectorItem={chooserItem}
                     labelKey='description'
                     valueKey='id'
                     disabled={!isEdit4}
                     onChange={this.handleSelectLoan}
                     value={loanSelectedList}
                     showNumber={formType === 2006}
                     single={formType === 2005}/>
            }
          </Col>
        </Row>
        {form.visibleLoanTypeScope !== 1004 && (
          <Row style={{marginTop: 12, marginBottom: 20}}>
            <Col span={8} offset={3}>
              {!isEdit4 && (
                <Button type="primary" onClick={() => this.handleChangeStatus('loan')}>{messages('common.edit')/*编辑*/}</Button>
              )}
              {isEdit4 && (
                <div>
                  <Button style={{marginRight: 20}} type="primary" loading={loading4} onClick={() => this.handleSave('loan')}>{messages('common.save')/*保存*/}</Button>
                  <Button onClick={() => this.handleChangeStatus('loan', true)}>{messages('common.cancel')/*取消*/}</Button>
                </div>
              )}
            </Col>
          </Row>
        )}
      </div>
    )
  };

  //公司模式下查看集团分配的表单
  renderSobLoanTypeScope = () => {
    return (
      <Row>
        <Col span={16} offset={3}>
          <div className="expense-type-list-wrap-title">
            <Icon type="info-circle"/>
            <span>{messages('setting.key1553')/*如需编辑可见借款类型，请联系集团管理员*/}</span>
          </div>
          <div className="expense-type-list-wrap">
            {this.renderLoanTypeScope(this.context.loanTypeScope)}
          </div>
        </Col>
      </Row>
    )
  };

  renderUser = (users) =>{
    if(users.visibleScope === 1001){
      return (
        <div>{messages('setting.key630'/*全公司人员*/)}</div>
      )
    }
    if(users.visibleScope === 1002){
      let len = users.userGroups ? users.userGroups.length : 0;
      return users.userGroups && users.userGroups.map((item,index) => {
        if(len - 1 === index){
          return (<div className="f-left">{item.name}</div>);
        }else {
          return (<div className="f-left">{item.name}，</div>);
        }
      })
    }
    if(users.visibleScope === 1003){
      return (
        <div>部门</div>
      )
    }
  }

  render() {
    const { loading1, isEdit1, loading2, isEdit2, loading3, isEdit3, companySelectedList, visibleCompanyScope,
      expenseSelectedList, visibleExpenseScope, userValue, canEditExpense } = this.state;
    const { formType, form } = this.context;
    return (
      <div className="form-permission">
        {this.props.tenantMode &&
          <div>
            <div className="info-title">{messages('setting.key631'/*分配公司*/)}</div>
            <Row>
              <Col span={8} offset={3}>
                <RadioGroup onChange={this.handleChangeCompany} value={visibleCompanyScope}>
                  <Radio disabled={!isEdit1} value={1}>{messages('setting.key633'/*全部公司*/)}</Radio>
                  <Radio disabled={!isEdit1} value={2}>{messages('setting.key634'/*部分公司*/)}</Radio>
                </RadioGroup>
                {visibleCompanyScope === 2 &&
                <Chooser placeholder={messages('common.please.select')}
                         type='available_company_setOfBooks'
                         labelKey='name'
                         valueKey='id'
                         disabled={!isEdit1}
                         onChange={this.handleSelectCompany}
                         value={companySelectedList}
                         listExtraParams={{setOfBooksId: this.context.booksID}}
                         showNumber={true}
                         single={false}/>
                }
              </Col>
            </Row>
            {form.visibleLoanTypeScope !== 1004 && (
              <Row style={{marginTop: 12, marginBottom: 20}}>
                <Col span={8} offset={3}>
                  {!isEdit1 && (
                    <Button type="primary" onClick={() => this.handleChangeStatus('company')}>{messages('common.edit')/*编辑*/}</Button>
                  )}
                  {isEdit1 && (
                    <div>
                      <Button style={{marginRight: 20}} type="primary" loading={loading1} onClick={() => this.handleSave('company')}>{messages('common.save')/*保存*/}</Button>
                      <Button onClick={() => this.handleChangeStatus('company', true)}>{messages('common.cancel')/*取消*/}</Button>
                    </div>
                  )}
                </Col>
              </Row>
            )}
          </div>
        }
        <div className="info-title">{messages('setting.key635'/*分配人员*/)}</div>
        {canEditExpense && (
          <div>
            {this.props.tenantMode || this.context.form.fromType != 2 ? this.renderTenantUser() : this.renderSobUser()}
          </div>
        )}
        {!canEditExpense && (
          <Row>
            <Col span={8} offset={3}>
              <div>
                {messages('setting.key636'/*注：关联报销单的分配人员与申请单一致，如需更改请在申请单页面操作*/)}
              </div>
            </Col>
          </Row>
        )}
        {!(formType === 2005 || formType === 2006) && (
          <div className="info-title">
            {messages('setting.key637'/*可见费用类型*/)}
          </div>
        )}
        {!(formType === 2005 || formType === 2006) && (
          <div>
            {this.props.tenantMode || this.context.form.fromType != 2 ? this.renderTenantExpenseTypeScope() : this.renderSobExpenseTypeScope()}
          </div>
        )}
        {(formType === 2005 || formType === 2006) && (
          <div className="info-title"> {messages('setting.key1554')/*借款类型*/}&nbsp;&nbsp;&nbsp;
            <span style={{fontSize: 14}} className='font-default'>{messages('setting.key1555')/*当前帐套下可启用的预付款现金事务分类*/}</span>
          </div>
        )}
        {(formType === 2005 || formType === 2006) && (
          <div>
            {this.props.tenantMode || this.context.form.fromType != 2 ? this.renderTenantLoanTypeScope() : this.renderSobLoanTypeScope()}
          </div>
        )}
      </div>
    )
  }
}

FormPermission.propTypes = {
  refreshData: React.PropTypes.func
};

function mapStateToProps(state) {
  return {
    company: state.login.company,
    language: state.main.language,
    tenantMode: state.main.tenantMode
  }
}

FormPermission.contextTypes = {
  formType: React.PropTypes.any,
  formOID: React.PropTypes.string,
  booksID: React.PropTypes.string,
  form: React.PropTypes.object,
  expenseTypeScope: React.PropTypes.object,
  loanTypeScope: React.PropTypes.object,
  userScope: React.PropTypes.object,
  propertyList: React.PropTypes.array,
  router: React.PropTypes.object
};

export default connect(mapStateToProps)(FormPermission)
